//
//  EmoticonViewModel.swift
//  03-表情键盘
//
//  Created by 房城鸿 on 15/10/18.
//  Copyright © 2015年 房兰峰. All rights reserved.
//

import UIKit

/**
从 Emoticon.bundle 中读取 emoticon.plist
遍历 packages 数组，创建EmoticonPackage 的数组
*/
/// 表情的视图模型  --》 加载表情数据
class EmoticonViewModel: NSObject {

    ///  表情包的数据
    lazy var package = [EmoticonPackage]()
    
    ///  加载表情包
    func loadPackages(){
        
        //0. 添加最近分组
        package.append(EmoticonPackage(dict: ["group_name_cn":"最近"]))
        
        //1. 读取 emoticons.plist
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        //2. 读取字典
        let dict = NSDictionary(contentsOfFile: path!)
        
        //3. 获取 packages 数组
        let array = dict!["packages"] as! [[String:AnyObject]]
        
        //4. 遍历数组，创建模型
        for infoDict in array{
            
            //1. 获取字典 id,目录中对应的 info.plist 才是表情包的数据
            let id = infoDict["id"] as! String
            
            //2.拼接表情包路径
            let emPath = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/" + id)
            
            //3.加载 info.plist 字典
            let packageDict = NSDictionary(contentsOfFile: emPath!) as! [String:AnyObject]
            
            //4. 字典转模型
            package.append(EmoticonPackage(dict: packageDict))
        }
//        print(package)
    }
    
    func emoticon(indexPath:NSIndexPath) -> Emoticon {
        
        return package[indexPath.section].emoticons[indexPath.item]
    }
}
